package com.crmweb.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.crmweb.dao.UsersDao;
import com.crmweb.dto.TreeModules;
import com.crmweb.entity.Modules;
import com.crmweb.entity.Roles;
import com.crmweb.entity.Userroles;
import com.crmweb.entity.Users;
import com.crmweb.service.UsersService;
@Service
public class UsersServiceImpl implements UsersService{

	@Resource
	private UsersDao usersDao;
	
	@Override
	public Users login(String loginName, String password) {
		// TODO Auto-generated method stub
		return usersDao.login(loginName, password);
	}

	@Override
	public List<String> selRoleIdByUid(String userId) {
		// TODO Auto-generated method stub
		return usersDao.selRoleIdByUid(userId);
	}

	@Override
	public List<Integer> selMidByRid(List<String> roleId) {
		// TODO Auto-generated method stub
		return usersDao.selMidByRid(roleId);
	}

	@Override
	public List<Modules> selByMid(List<Integer> mids) {
		// TODO Auto-generated method stub
		return usersDao.selByMid(mids);
	}

	/**
	 * 根据登录的用户获取相对应的模块信息
	 * */
	@Override
	public List<Modules> queryByLoginUser(String loginName, String password) {
		// TODO Auto-generated method stub
		Users users=login(loginName, password);
		if(users!=null) {
			List<String> rids=selRoleIdByUid(users.getId());
			if(rids.size()>0) {
				List<Integer> mids=selMidByRid(rids);
				if(mids.size()>0) {
					List<Modules> list=selByMid(mids);
					return list;
				}
			}
		}
		return null;
	}

	/**
	 * 将modules里面的属性替换成树节点
	 * **/
	@Override
	public List<TreeModules> queryModulesTreeNodes(Users users) {
		// TODO Auto-generated method stub
		List<TreeModules> NodesList=new ArrayList<TreeModules>();
		List<Modules> list=queryByLoginUser(users.getLoginName(), users.getPassword());
		for (Modules modules : list) {
			TreeModules treeNodes=new TreeModules();
			treeNodes.setId(modules.getId());
			treeNodes.setName(modules.getName());
			treeNodes.setPath(modules.getPath());
			treeNodes.setParentId(String.valueOf(modules.getParentId()));
			NodesList.add(treeNodes);
		}
		return NodesList;
	}

	
	/**
	 * 封装成树形
	 * */
	@Override
	public List<TreeModules> queryModel(List<TreeModules> nodes) {
		// TODO Auto-generated method stub
		//声明list集合存放以及目录
		List<TreeModules> rootList=new ArrayList<TreeModules>();
		for (TreeModules treeModules : nodes) {
			if(treeModules.getParentId().equals("0")) {
				rootList.add(treeModules);
			}
		}
		
		//存放二级目录
		for (TreeModules tree : rootList) {
			tree.setChildren(getChild(String.valueOf(tree.getId()), nodes));
		}
		return rootList;
	}

	//获取孩子的迭代算法
	public List<TreeModules> getChild(String parentId,List<TreeModules> list){
		//声明孩子存储集合
		List<TreeModules> childList=new ArrayList<TreeModules>();
		for (TreeModules tree : list) {
			if(tree.getParentId().equals(parentId)) {
				childList.add(tree);
			}
		}
		
		//存储孩子节点内容
		for (TreeModules mtree : childList) {
			mtree.setChildren(getChild(String.valueOf(mtree.getId()), list));
		}
		
		return childList;
	}

	@Override
	public List<String> findAllName() {
		// TODO Auto-generated method stub
		return usersDao.findAllName();
	}

	@Override
	public int updateLastLoginTime(String id) {
		// TODO Auto-generated method stub
		return usersDao.updateLastLoginTime(id);
	}

	@Override
	public List<Users> showAllByWhere(Users user) {
		// TODO Auto-generated method stub
		return usersDao.showAllByWhere(user);
	}

	@Override
	public int getCount(Users user) {
		// TODO Auto-generated method stub
		return usersDao.getCount(user);
	}

	@Override
	public int addUser(Users user) {
		// TODO Auto-generated method stub
		return usersDao.addUser(user);
	}

	@Override
	public int updUser(Users user) {
		// TODO Auto-generated method stub
		return usersDao.updUser(user);
	}

	@Override
	public int delUser(String id) {
		// TODO Auto-generated method stub
		return usersDao.delUser(id);
	}

	@Override
	public int deblockUser(String id) {
		// TODO Auto-generated method stub
		return usersDao.deblockUser(id);
	}

	@Override
	public int addRolersToUser(List<Userroles> rid) {
		// TODO Auto-generated method stub
		return usersDao.addRolersToUser(rid);
	}

	@Override
	public int delRolesByUser(String rid, String uid) {
		// TODO Auto-generated method stub
		return usersDao.delRolesByUser(rid, uid);
	}

	@Override
	public int blockUser(String id) {
		// TODO Auto-generated method stub
		return usersDao.blockUser(id);
	}

	@Override
	public int updPassword(String pwd, String uid) {
		// TODO Auto-generated method stub
		return usersDao.updPassword(pwd, uid);
	}

	@Override
	public Roles selRoleByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return usersDao.selRoleByRoleId(roleId);
	}

	@Override
	public Users selUsersByUid(String id) {
		// TODO Auto-generated method stub
		return usersDao.selUsersByUid(id);
	}
	
	


}
